package app.qrcode.browser.ykpixabay.logic

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Environment
import android.widget.Toast
import androidx.core.graphics.PathUtils
import androidx.lifecycle.liveData
import app.qrcode.browser.ykpixabay.MyApplication.Companion.context
import app.qrcode.browser.ykpixabay.logic.network.PixNetwork
import kotlinx.coroutines.Dispatchers
import java.io.File
import java.io.FileOutputStream
import java.io.OutputStream
import java.lang.Exception
import java.lang.RuntimeException
import java.util.*
import kotlin.contracts.contract

/**
 * Created by YiGo on 2022/4/18.
 */
object Repository {
    fun searchPic(query: String) = liveData(Dispatchers.IO) {
        val result = try {
            val picResponse = PixNetwork.searchPic(query)
            if (picResponse.total != 0) {
                //执行转换逻辑，将api获取数据转化成自己需要的数据
                val pic = picResponse.hits
                Result.success(pic)
            } else {
                Result.failure(RuntimeException("response total is ${picResponse.total}"))
            }
        } catch (e: Exception) {
            Result.failure(e)

        }
        emit(result)
    }


}